package com.scuec.groupfive.Controller;

import com.scuec.groupfive.entity.*;
import com.scuec.groupfive.service.RollManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/roll")
public class RollManagementController {

    @Autowired
    private RollManagementService rollManagementService;

    /**
     * 根据年份和试卷类型获取真题题目
     * @param examYear 年份
     * @param typeId 试卷类型ID (1:数一, 2:数二, 3:数三)
     * @return 按题目类型排序的题目列表
     */
    @GetMapping("/questions")
    public List<QuestionInfo> getQuestionsByYearAndType(
            @RequestParam Integer examYear,  // 这里已经是examYear，不需要修改
            @RequestParam Integer typeId) {
        return rollManagementService.getQuestionsByYearAndType(examYear, typeId);
    }

    /**
     * 更新题目基本信息
     * @param questionInfo 题目信息
     * @return 更新后的题目信息
     */
    @PutMapping("/question")
    public ResponseEntity<QuestionInfo> updateQuestion(@RequestBody QuestionInfo questionInfo) {
        QuestionInfo updatedQuestion = rollManagementService.updateQuestion(questionInfo);
        return ResponseEntity.ok(updatedQuestion);
    }

    /**
     * 统一答案更新接口
     * @param questionInfo 包含答案信息的题目对象
     * @return 操作结果
     */
    @PutMapping("/answer")
    public ResponseEntity<Map<String, Object>> updateAnswer(@RequestBody QuestionInfo questionInfo) {
        Map<String, Object> response = new HashMap<>();

        try {
            switch (questionInfo.getQuestionType()) {
                case "single_choice":
                case "multiple_choice":
                    ChoiceQuestion cq = new ChoiceQuestion();
                    cq.setQuestionId(questionInfo.getQuestionId());
                    cq.setOptions(questionInfo.getOptions());
                    cq.setAnswer(questionInfo.getChoiceAnswer());
                    cq.setAnalysis(questionInfo.getChoiceAnalysis());
                    rollManagementService.updateChoiceAnswer(cq);
                    break;

                case "fill_in_the_blank":
                    FillBlankQuestion fbq = new FillBlankQuestion();
                    fbq.setQuestionId(questionInfo.getQuestionId());
                    fbq.setBlanks(questionInfo.getBlanks());
                    fbq.setAnswerTemplate(questionInfo.getAnswerTemplate());
                    fbq.setAnalysis(questionInfo.getFillblankAnalysis());
                    rollManagementService.updateFillBlankAnswer(fbq);
                    break;

                case "subjective":
                    SubjectiveQuestion sq = new SubjectiveQuestion();
                    sq.setQuestionId(questionInfo.getQuestionId());
                    sq.setReferenceAnswer(questionInfo.getReferenceAnswer());
                    sq.setAnswerHints(questionInfo.getAnswerHints());
                    sq.setAnalysis(questionInfo.getSubjectiveAnalysis());
                    rollManagementService.updateSubjectiveAnswer(sq);
                    break;
            }

            response.put("code", 200);
            response.put("message", "修改成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("code", 500);
            response.put("message", "修改失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
}